﻿$(function () {
    var currentPage = 0;
    var orderType = getQueryString("orderType");
    var orderTotlaPage = $("#orderTotlaPage").html();
 
    $("#orderType option[value='" + orderType + "']").attr("selected", true);
    var $selectOrderTypeText = $('#orderType option:selected').text();
    $("#labOrderType").html($selectOrderTypeText);
   
    //订单类型改变
    $("#orderType").change(function () {
        var selectOrderType = $('#orderType option:selected').val();
        window.location.href = 'OrderList?orderType=' + selectOrderType;
    });

    //得到query string
    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }    
    contrlPageNumberDisplay();
    /*-------------分页---------------*/
    //控制页码是否显示
    function contrlPageNumberDisplay() {
        $('#currentPage').html(currentPage + 1);
        //当处于首页的时候,'上一页'隐藏
        if (currentPage == 0) {
            $('#previousPage').hide();
        }
        else {
            $('#previousPage').show();
        }
        //当处于末页的时候,'下一页'隐藏
        if (currentPage == (orderTotlaPage - 1)) {
            $('#nextPage').hide();
        }
        else {
            $('#nextPage').show();
        }
    }

            
    //首页
    var $fristPage = $("#fristPage");
    $fristPage.click(function (e) {
        $.get("OrderListByAjaxRequest?orderType=" + orderType + "&pageIndex=0", function (data) {
            currentPage = 0;
            $('#orderList').empty();
            $('#orderList').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

    //上一页
    var $previousPage = $("#previousPage");
    $previousPage.click(function (e) {
        $.get("OrderListByAjaxRequest?orderType=" + orderType + "&pageIndex=" + (currentPage - 1), function (data) {
            currentPage = currentPage - 1;
            $('#orderList').empty();
            $('#orderList').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

    //下一页
    var $nextPage = $("#nextPage");
    $nextPage.click(function (e) {
        $.get("OrderListByAjaxRequest?orderType=" + orderType + "&pageIndex=" + (currentPage + 1), function (data) {
            currentPage = currentPage + 1;
            $('#orderList').empty();
            $('#orderList').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

    //末页
    var $lastPage = $("#lastPage");
    $lastPage.click(function (e) {
        $.get("OrderListByAjaxRequest?orderType=" + orderType + "&pageIndex=" + (orderTotlaPage - 1), function (data) {
            currentPage = orderTotlaPage - 1;
            $('#orderList').empty();
            $('#orderList').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

});

//打开取消订单的窗口
function opneCancelOrderWindow(orderNumber) {
    $('#orderNumber').val(orderNumber);
    $.fancybox.open({
        href: "#cancelOrderWindow",
        maxWidth: 1000,
        maxHeight: 600,
        fitToView: true,
        width: 700,
        height: 400,
        wrapCSS: 'custom-fancy-wrapper',
        helpers: {
            overlay: {
                css: {
                    'background': 'rgba(255,255,255,.6)'
                },
                closeClick: false
            }
        },
        tpl: {
            closeBtn: '<i class="fa fa-times"></i>'
        },
        autoSize: false
    });
}

//取消订单
function cancelOrder() {
    var orderNumber = $('#orderNumber').val();
    var orderVoidType = $("#voidOrderType").val();
    var orderVoidReason = $.trim($("#voidReason").val());
   
    if (!orderVoidType) {
        KevinShop.MessageBox.Alert('请选择取消原因！');
        return;
    }

    if (orderVoidType == 7
        && (orderVoidReason == null
        || orderVoidReason == '')) {
        KevinShop.MessageBox.Alert('请输入作废备注！');
        return;
    }

    KevinShop.MessageBox.Confirm("", "您确定取消订单吗？", function (isConfirm) {
        if (isConfirm) {
            var url = "VoidOrder";
            var voidOrder = {};
            voidOrder["orderNumber"] = orderNumber;
            voidOrder["voidType"] = orderVoidType;
            voidOrder["voidReason"] = orderVoidReason;
            KevinShop.Common.Ajax.Post(url, voidOrder, function (data) {
                if (data.error) {
                    KevinShop.MessageBox.Alert(data.error);
                    return;
                }
                if (!data.Status) {
                    KevinShop.MessageBox.Alert(data.ErrorMessage);
                    return;
                }
                var url = "/MyAccount/OrderList";
                window.location = url + "?orderType=3";
            }, false);
        }
    });
}

